//
//  CTLogicViewController.m
//  CT
//
//  Created by liufanwei on 15/4/4.
//  Copyright (c) 2015年 liufanwei. All rights reserved.
//

#import "CTLoginViewController.h"
#import "UILabel+WBExtension.h"
#import "CTNetHelper.h"
#import "BDKNotifyHUD.h"
#import "CommonMicro.h"
#import "CTUserDefaultHelper.h"
#import "AppDelegate.h"
#import "UIView+WBExtension.h"
#import "AFNetworking.h"

@interface CTLoginViewController ()<UITextFieldDelegate>
{
    UITextField * _PhoneNumber;
    UITextField * _PhonePWD;
    
    UIButton    * _ButtonLogin;
    UIButton    * _ButtonRegister;
    UIImageView * _SmallBg;
}

@end

@implementation CTLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBar.barTintColor = kBackGroundColor;
    UILabel * title = [UILabel labelWithText: @"智云通用户登录" font:[UIFont fontWithName:kFontName size:20]];
    title.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = title;
    
    CGRect navigationbarframe = self.navigationController.navigationBar.frame;
    

    CGFloat y = navigationbarframe.origin.y + navigationbarframe.size.height + 10;
    CGFloat x = 25;
    _SmallBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"inputBG"]];
    _SmallBg.userInteractionEnabled = YES;
    [self.view addSubview:_SmallBg];
    CGRect frame = _SmallBg.frame;
    frame.origin.x = x;
    frame.size.width = kAppFrameWidth - 2 * x;
    frame.origin.y = y;
    _SmallBg.frame = frame;
    
    CGFloat textfieldheight = (_SmallBg.frame.size.height - 3 * 6)/2;
    _PhoneNumber = [[UITextField alloc] initWithFrame:CGRectMake(6, 6, _SmallBg.frame.size.width - 12, textfieldheight)];
    UIImageView * numberLeftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"head"]];
    _PhoneNumber.leftView = numberLeftView;
    _PhoneNumber.leftViewMode = UITextFieldViewModeAlways;
    _PhoneNumber.placeholder = @"已注册手机号码";
    _PhoneNumber.backgroundColor = [UIColor whiteColor];
    _PhoneNumber.keyboardType = UIKeyboardTypeNumberPad;
    _PhoneNumber.delegate = self;
    _PhoneNumber.enabled = YES;
    [_PhoneNumber setFont:[UIFont fontWithName:kFontName size:21]];
    [_SmallBg addSubview:_PhoneNumber];
    
    _PhonePWD = [[UITextField alloc] initWithFrame:CGRectMake(6, 6+6+textfieldheight, _SmallBg.frame.size.width - 12, textfieldheight)];
    _PhonePWD.leftViewMode = UITextFieldViewModeAlways;
    _PhonePWD.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"password"]];
    _PhonePWD.backgroundColor = [UIColor whiteColor];
    _PhonePWD.placeholder = @"登录密码";
    _PhonePWD.delegate = self;
    [_PhonePWD setFont:[UIFont fontWithName:kFontName size:21]];
    _PhonePWD.secureTextEntry = YES;
    [_SmallBg addSubview:_PhonePWD];
    
    CGFloat buttonWidth = _SmallBg.frame.size.width/2 - 15 ;
    CGFloat buttony = _SmallBg.frame.origin.y + _SmallBg.frame.size.height + 20;
    CGFloat buttonHeight = 45;
    _ButtonLogin = [[UIButton alloc] init];
    [_ButtonLogin setBackgroundImage:[UIImage imageNamed:@"btnlogin"] forState:UIControlStateNormal];
    _ButtonLogin.frame = CGRectMake(x, buttony, buttonWidth, buttonHeight);
    [_ButtonLogin addTarget:self action:@selector(onLoginClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_ButtonLogin];


    _ButtonRegister = [[UIButton alloc] init];
    [_ButtonRegister setBackgroundImage:[UIImage imageNamed:@"btnreg"] forState:UIControlStateNormal];
    _ButtonRegister.frame = CGRectMake((x + _SmallBg.frame.size.width - buttonWidth), buttony, buttonWidth, buttonHeight);
    [_ButtonRegister addTarget:self action:@selector(onRegister:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_ButtonRegister];
    
    
    buttony += _ButtonRegister.frame.size.height;
    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake((x + _SmallBg.frame.size.width - buttonWidth), buttony, buttonWidth, buttonHeight)];
    [button setTitle:@"忘记密码?" forState:UIControlStateNormal];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(onResetPassword:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField.text.length > 10 && textField == _PhoneNumber && string.length != 0) {
        return NO;
    }
    if (textField.text.length > 11 && textField == _PhonePWD && string.length != 0) {
        return NO;
    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)onRegister:(id)sender{
    [self performSegueWithIdentifier:@"logintoregister" sender:self];
}

-(void)onResetPassword:(id)sender{
    [self performSegueWithIdentifier:@"resetpassword" sender:self];
}

- (void)onLoginClick:(id)sender {
    NSString * acctount = _PhoneNumber.text;
    NSString * pwd = _PhonePWD.text;
    if(acctount.length != 11 || acctount.intValue == 0){
        [BDKNotifyHUD notifyHUDWithText:@"非法账号！账号为你注册手机号"];
        return;
    }
    
    if (pwd.length < 6) {
        [BDKNotifyHUD notifyHUDWithText:@"密码过短"];
        return;
    }
    
    [[CTNetHelper ShareInstance] login:@{@"account":acctount,@"password":pwd}
                          WithCallBack:^(id data) {
                              if (![data isKindOfClass:[NSDictionary class]]) {
                                  return ;
                              }
                              NSDictionary * dic = (NSDictionary*)data;
                              [BDKNotifyHUD notifyHUDWithText:[dic objectForKey:@"msg"]];
                              if ([[dic objectForKey:@"code"] integerValue] == 0) {
                                  [[CTUserDefaultHelper ShareInstance] setAccount:acctount];
                                  [kAppDelegate ShowMain];
                              }
                          }];
}



/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
